(function() { 
	appCommon.controller('contentCtr', contentCtroller);	

	function contentCtroller($scope, $http, $httpParamSerializer, $compile, ngDialog) { 
        var post_id = location.href.split("post_id=")[1];
        $scope.items = [];
        $scope.noMore = "加载中...";
        var nowPage = 2;
        var count = 5;        
        var init = false;
        var busy = false;

        $http({
            method: 'POST',
            url: '/index.php?m=home&a=index&c=content',           
            data: $httpParamSerializer({
                        'post_id': post_id
                    }),
            headers:{'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function successCallback(response) {
            var data = response.data;
            if(data.status) {
                $scope.uid = data.uid;
                $scope.result = data.content[0];
                $scope.result.comments = data.comments;
                $scope.result.isGood = data.isGood;
                $scope.result.username = data.username;
                $scope.result.photo = data.photo;
                $scope.result.goodCount = data.goodCount; 
                $scope.metext = {};   
                $scope.nowIndex = {};   

                data.comments.length > 4 || ($scope.noMore = "没有更多...")
                // 编译html
                var html = $compile($scope.result["post_content"].replace(/\\"/g, '"'))($scope);
                // 插入到dom
                document.getElementsByClassName('post-content')[0].appendChild(html[0]);
                //先加载文章，再加载评论
                init = true;
            }else {
               // window.location.href = "/#/index";
            }
        }, function errorCallback(response) {
            console.log(response)
        });

        //获取评论列表
        $scope.loadComment = function(post_id) {  
            //初次已经默认加载            
            if(!init) return;
            //防止重复加载
            if (busy) return;
            busy = true;
            $http({
                method: 'POST',
                url: '/index.php?m=home&a=getComment&c=content',           
                data: $httpParamSerializer({
                            'post_id': post_id,
                            'nowPage': nowPage,
                            'count'  : count
                        }),
                headers:{'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function successCallback(response) {
                nowPage++;
                var items = response.data.commentList;
                for (var i = 0; i < items.length; i++) {
                    $scope.result.comments.push(items[i]);
                }

                if(items.length > 3) {                   
                    busy = false;
                }else {
                    $scope.noMore = "没有更多...";
                    busy = true;
                }
            }, function errorCallback(response) {
                console.log(response)
            });       
        }

        //回复
        $scope.dialog = false;        
        $scope.comment = function(index, username, author, photo, comment_uid, comment_reply_id) { 
            var text =  index === false ? $scope.meText : $scope.metext['text'+index]; 
            if(comment_uid) {
                $http({
                    method: 'POST',
                    url: '/index.php?m=home&a=comment&c=content',           
                    data: $httpParamSerializer({
                        "post_id" : $scope.result.post_id,
                        "text" : text,
                        'username' : username, 
                        'author' : author,
                        'comment_uid': comment_uid,
                        'comment_reply_id': comment_reply_id,
                        'photo' : photo
                    }),
                    headers:{'Content-Type': 'application/x-www-form-urlencoded'}
                }).then(function successCallback(response) {
                    if(response.data.comment_id) {
                        $scope.result.comments.unshift({
                            comment_username : username,
                            comment_author: author,
                            comment_text: text,
                            comment_photo: photo,
                            isCommentGood: 'bad',
                            comment_good_count: 0,
                            comment_id: response.data.comment_id,
                            comment_uid: comment_uid
                        });

                        $scope.meText = '';
                        $scope.metext['text'+index] = '';
                        $scope.isShow = '';
                        $scope.nowIndex['index'+index] = '';
                    }
                }, function errorCallback(response) {
                    console.log(response)
                });  
            } else {                
                if(!$scope.dialog) {
                    $scope.dialog = ngDialog.open({      
                        plain: true,                    
                        template: '<div class="save-massage">请先 <a href=" '+ location.origin + '/#/login">登录</a> </div>', 
                        controller: 'registerCtr',
                        overlay: false
                    });
                }                
            }            
        }

        // 删除评论
        $scope.delComment = function(comment_id) {
            if(comment_id) {
                $http({
                    method: 'POST',
                    url: '/index.php?m=home&a=delcomment&c=content',           
                    data: $httpParamSerializer({
                        'comment_id': comment_id,
                    }),
                    headers:{'Content-Type': 'application/x-www-form-urlencoded'}
                }).then(function successCallback(response) {
                    if(response.data) {
                        $scope.result.comments = $scope.result.comments.filter(function(i){
                            return i.comment_id != comment_id
                        })
                    } else {
                        $scope.dialog = ngDialog.open({      
                            plain: true,                    
                            template: '<div class="save-massage">删除失败</div>', 
                            controller: 'registerCtr',
                            overlay: false
                        });
                    }
                }, function errorCallback(response) {
                    console.log(response)
                }); 
            } 
        }

        //评论点赞
        $scope.commentGood = function(item) {  
            $http({
                method: 'POST',
                url: '/index.php?m=home&a=commentGood&c=content',           
                data: $httpParamSerializer({
                            'comment_id': item.comment_id
                        }),
                headers:{'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function successCallback(response) {
                //未登录
                if(response.data.type == null) {
                    if(!$scope.dialog) {
                        $scope.dialog = ngDialog.open({      
                            plain: true,                    
                            template: '<div class="save-massage">请先 <a href=" '+ location.origin + '/#/login">登录</a> </div>', 
                            controller: 'registerCtr',
                            overlay: false
                        });
                    }
                }else {
                    item.isCommentGood = response.data.type;
                    item.comment_good_count = 
                    response.data.type == 'good' ? (parseInt(item.comment_good_count)+ 1) : (item.comment_good_count - 1)
                }
            }, function errorCallback(response) {
                console.log(response)
            });       
        }

        //文章点赞
        $scope.good = function(item) {
            $http({
                method: 'POST',
                url: '/index.php?m=home&a=good&c=index',           
                data: $httpParamSerializer({
                            "post_id" :  item.post_id,
                        }),
                headers:{'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function successCallback(response) {
                //未登录
                if(response.data.type == null) {
                    window.location.href="/#/login";
                }else {
                    item.isGood = response.data.type;
                    item.goodCount = response.data.goodCount;
                }
            }, function errorCallback(response) {
                console.log(response)
            });     
        }
	}
})();